//
//  WelcomeViewController.swift
//  SinaBlog
//
//  Created by macs on 15/11/12.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        setAnimation()
    }
    
    //设置动画
    private func setAnimation() {
        iconView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-self.view.bounds.height + 180)
        }
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            //
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(1, animations: { () -> Void in
                    //
                    self.lable.alpha = 1
                    }, completion: { (_) -> Void in
                        //
                        NSNotificationCenter.defaultCenter().postNotificationName(notityKey, object: nil, userInfo: nil)
                })
        }
        
    }
    
    //2.添加控件,设置 frame
    private func setupUI() {
        
        view.addSubview(iconView)
        view.addSubview(lable)
        
        //布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view.snp_centerX)
            make.bottom.equalTo(view.snp_bottom).offset(-180)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        lable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(20)
        }
        
        iconView.sd_setImageWithURL(AccessViewModel().avatarUrl, placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 45
        iconView.layer.masksToBounds = true
        lable.alpha = 0
    }
    
    //1.懒加载控件
    lazy var backView = UIImageView(image: (UIImage(named: "ad_background")))
    lazy var iconView : UIImageView = UIImageView()
    lazy var lable : UILabel = UILabel(title: (AccessViewModel().name ?? "") + " 欢迎回来", size: 19, color: UIColor.lightGrayColor())
    

    
    
}
